# -*- coding: utf-8 -*-
"""
Created on Tue Jun 16 06:34:58 2020

@author: Farman
"""

import os


url_file = 'LandsatDataFilesToDown.list'
local_file = 'LandsatDataFilesAlreadyDown.list'
save_path  = r'E:\Landsat'

with open(url_file) as f:
    urls = f.readlines()

urls = [url.rstrip() for url in urls]

for n in range(len(urls)):
    url = urls[n]
    
    new_url = url.replace('/data/yule', 'http://12.2.5.5:61235')
    command = 'aria2c -c %s --dir=%s'%(new_url, save_path)
    #print(command)
    #continue
    
    ret = os.system(command)
    
    if ret == 0:
        try:
            with open(local_file, 'at') as f:
                f.write(url + '\n')
        except:
            with open(local_file, 'wt') as f:
                f.write(url + '\n')
            
        urls = urls[n+1:]
        
        with open(url_file, 'wt') as f:
            f.write('\n'.join(urls))
    else:
        urls = urls[n+1:]
        urls.append(url)
        
        with open(url_file, 'wt') as f:
            f.write('\n'.join(urls))
